package com.example.ui_view.service;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.JobIntentService;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.ui_view.R;

public class MyIntentActivity extends AppCompatActivity {
    Button btn_startService,btn_stopService,btn_jonservice,btn_notifi;
    ResultReceiver resultReceiver = new ResultReceiver(new Handler()){
        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            super.onReceiveResult(resultCode, resultData);
            if(resultCode == 0x111){
                Toast.makeText(MyIntentActivity.this, resultData.getString("str_key"), Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_intent);
        btn_startService = findViewById(R.id.btn_startService);
        btn_stopService = findViewById(R.id.btn_stopService);
        btn_jonservice = findViewById(R.id.btn_jonservice);
        btn_notifi = findViewById(R.id.btn_notifi);

        btn_startService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyIntentActivity.this,MyService.class);
                Log.v("msg","点击了按钮");
                intent.putExtra("str_name","传递数据。。。");
                startService(intent);
            }
        });

        btn_stopService.setOnClickListener(new View.OnClickListener() {
            Intent intent = new Intent(MyIntentActivity.this,MyService.class);
            @Override
            public void onClick(View v) {
                stopService(intent);
            }
        });



        btn_jonservice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.putExtra("str_name","jobservice的服务");
                intent.putExtra("receiver_key",resultReceiver);
                JobIntentService.enqueueWork(MyIntentActivity.this,MyJobIntentService.class,0x110,intent);
            }
        });

        btn_notifi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                JobIntentService.enqueueWork(MyIntentActivity.this,MyNotifiJobIntentService.class,0x111,intent);
            }
        });

    }

}
